package algorithm.binary;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-06 15:36
 * @ Description :
 */
public class BinarySearch
{
    public static void main(String[] args)
    {
        int[] array = {1, 3, 8, 10, 11, 67, 100};
        int index = binarySearch(array,19);
        System.out.println("index = " + index);
    }

    //array: 升序数组
    public static int binarySearch(int[] array, int findValue)
    {
        int left = 0;
        int right = array.length - 1;
        while (left <= right)
        {
            int mid = (left + right) / 2;
            if (array[mid] == findValue)
            {
                return mid;
            }
            else if (array[mid] > findValue)
            {
                right = mid - 1;
            }
            else
            {
                left = mid + 1;
            }
        }
        return -1;
    }
}
